/*
 * Copyright 2008 Carnegie Mellon University
 * Licensed under the Apache License, Version 2.0 (the "License"); 
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *  
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, 
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
package edu.cmu.lti.sepia.data.session;

import java.sql.SQLException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import edu.cmu.lti.sepia.data.db.SQL;
import edu.cmu.lti.sepia.data.db.TaskDAO;
import edu.cmu.lti.sepia.data.db.UserDAO;
import edu.cmu.lti.sepia.infra.system.model.User;
import edu.cmu.lti.sepia.infra.topic.model.Task;

/**
 * 
 * Manages data associated with a session.
 * 
 * @author Hideki Shima
 *
 */
public class SessionManager {

	public static void storeUser( SQL sql, String userName, HttpSession session ) {
		try {
			User user = UserDAO.findByUserName( sql, userName );
			session.setAttribute("userId", user.getUserId());
			session.setAttribute("userName", user.getUserName());
			session.setAttribute("userRole", user.getRole());
			session.setAttribute("displayLang", user.getDisplayLang());
			session.setAttribute("isAdmin", user.getRole().equals("admin"));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public static void storeTask( SQL sql, String taskNameShort, HttpSession session ) {
		try {
			Task task = TaskDAO.findByTaskNameShort( sql, taskNameShort );
			if (task!=null) {
  			session.setAttribute("taskName", task.getTaskName());
  			session.setAttribute("taskId", task.getTaskId());
  			session.setAttribute("taskNameShort", task.getTaskNameShort());
  			session.setAttribute("taskDescription", task.getDescription());
  			session.setAttribute("indices", task.getIndices());
  			session.setAttribute("isMonolingual", task.isMonolingual());
  			session.setAttribute("srcLang", task.getSrcLang());
  			session.setAttribute("trgLang", task.getTrgLang());
  			session.setAttribute("types", task.getTypes());
  			session.setAttribute("tdLabels", task.getTdLabels());
  			session.setAttribute("irLabels", task.getIrLabels());  			
			}
		} catch ( SQLException e ) {
			e.printStackTrace();
		}
	}
	
	public static void storeHost( HttpServletRequest request, HttpSession session ) {
		session.setAttribute("host", request.getRemoteHost());
	}
	
	public static void resetTask( HttpSession session ) {
		session.setAttribute("taskName", null);
		session.setAttribute("taskId", null);
		session.setAttribute("taskNameShort", "none");
    session.setAttribute("taskDescription", null);
		session.setAttribute("indices", null);
		session.setAttribute("isMonolingual", null);
		session.setAttribute("srcLang", null);
		session.setAttribute("trgLang", null);
		session.setAttribute("types", null);
		session.setAttribute("irLabels", null);
		session.setAttribute("tdLabels", null);
	}
}
